Skip to content

Conversation

@karlseguin
Copy link
Collaborator

@karlseguin karlseguin commented Mar 14, 2025

The two main goals are:
1 - Have a single HTTP client that does both sync and async. This makes it easier
to add things like proxy support, and might help with things like cookies.

2 - Maintaining our own HTTP client isn't trivial, especially given that
we're likely to encounter non-conforming servers. But, this removes the need
to maintain zig-async-io, which is a fork of [a small part] of the stdlib and
tls.zig. This itself is non-trivial. Also, if we do run into weird server
behavior, we'd have to maintain that in std.http.Client unless we could get
a PR accepted.

What's currently missing
1 - Redirect handling (critical to add this)
2 - Chunked encoding response (critical to add this)
3 - Connection pooling (could be added after)
4 - Compression (could be added after)

If you try "fetch" or "serve" a page that doesn't do a direct and that doesn't
use chunked encoding, it should work. Puppeteer demo works.

This is built on the zig-0.14 branch. I initially started working against main
(Zig 0.13) but then I noticed that tls.zig has very useful support for a
generic async loop in its master. It's a pretty big game changer and makes the
integration with our event loop trivial (I'm currently pointing to my own fork,
but I'm hopeful that my PR (ianic/tls.zig#8)
will be accepted, and if not, it's a simple change to accommodate tls.zig as-is).

The async API is simpler, with as single callback. A simplified example:

fn send (self *XMLHttpRequest, loop: *Loop) !void {
    const request = try self.client.request(self.method, self.uri);
    errdefer request.deinit();

    for (self.headers.list.items) |hdr| {
        try request.addHeader(hdr.name, hdr.value, .{});
    }
    request.body = self.payload;
    try request.sendAsync(loop, self, .{});
}

pub fn onHttpResponse(self: *XMLHttpRequest, progress_: http.Error!http.Progress) !void {
      const progress = try progress_;

      if (progress.first) {
          // the first emitted chunk,
          // get the headers, get the status
      }

      if (progress.data) |data| {
          // we have part of the body,
          // can still get the headers/status here if needed
          self.body.append(data);
      }

      if (progress.done) {
        // we're done!
      }
}

Generally, the integration with browser an XMLHttpRequest was rushed. The focus
has been on src/http/client.zig.

Finally, I've added the tls.zig dependency as a Zig package (build.zig.zon)
rather than a submodule. I assume submodules were chosen back when Zig didn't
have a package manager.

@karlseguin karlseguin force-pushed the http_client branch 2 times, most recently from a60ceaf to 59f4e45 Compare March 14, 2025 12:14
@karlseguin karlseguin force-pushed the zig-0.14 branch 2 times, most recently from 063d2b5 to fe14b65 Compare March 18, 2025 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants